package org.wing.mouseListener;

import org.come.Frame.TestpackJframe;
import org.come.Frame.ZhuFrame;
import org.come.entity.Goodstable;
import org.come.until.GoodsListFromServerUntil;
import org.wing.panel.LHMainPanel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;

public class LHUpStarMouseListener extends MouseAdapter {

    private int type;
    private LHMainPanel LHMainPanel;

    public LHUpStarMouseListener(int type, LHMainPanel LHMainPanel) {
        this.type = type;
        this.LHMainPanel = LHMainPanel;
    }

    @Override
    public void mousePressed(MouseEvent e) {

        Goodstable wingUpStarGoods = GoodsListFromServerUntil.getWingUpStarGoods(type);
        if (wingUpStarGoods == null) {
            return;
        }
        if (LHMainPanel.getWingGoods() == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("没有选中翅膀");
            return;
        }

        Goodstable goodstable = GoodsListFromServerUntil.getRgid(LHMainPanel.getWingGoods());
        if (goodstable == null) {
            return;
        }
        String[] split = goodstable.getValue().split("\\|");
        Integer goodsValue = Integer.parseInt(LHMainPanel.getGoodsValue(split, "星级"));
        LHMainPanel.setMoney(getGoodsTrue(wingUpStarGoods, goodsValue));
        if (LHMainPanel.getMoney() == null) {
            // 不符合
            ZhuFrame.getZhuJpanel().addPrompt2("不符合该星级");
            return;
        }
        String goodsQuality = LHMainPanel.getGoodsValue(split, "品质");
        String qualityNumber = getGoodsQuality(goodsQuality);
        if (qualityNumber == null) {
            // 品质不正确
            ZhuFrame.getZhuJpanel().addPrompt2("不符合该品质");
            return;
        }

        LHMainPanel.setChosegoods(wingUpStarGoods);
        LHMainPanel.getChooseGoodsImg().setIcon(
                GoodsListFromServerUntil.imgpathAdaptive(LHMainPanel.getChosegoods().getSkin(), 58, 56));

        LHMainPanel.getNumberLab().setText(qualityNumber);

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        Goodstable goodstable = GoodsListFromServerUntil.getWingUpStarGoods(type);
        if (goodstable != null) {
            // TestpackJapnel.PaintingText(goodPlace);
            ZhuFrame.getZhuJpanel().creatgoodtext(goodstable);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        TestpackJframe.getTestpackJframe().getJpac().ClearText();
        ZhuFrame.getZhuJpanel().cleargoodtext();
    }

    // 判断当前选中的材料是否当前星级
    public static BigDecimal getGoodsTrue(Goodstable wingUpStarGoods, Integer goodsValue) {
        if (goodsValue == null) {
            return null;
        }
        String value = wingUpStarGoods.getValue().split("=")[1];
        if (goodsValue <= 2) {
            if (value.equals("1")) {
                // 不符合要求
                return new BigDecimal("10000000");
            }
        } else if (goodsValue <= 5) {
            if (value.equals("2")) {
                // 不符合要求
                return new BigDecimal("20000000");
            }
        } else if (goodsValue <= 8) {
            if (value.equals("3")) {
                // 不符合要求
                return new BigDecimal("30000000");
            }
        } else if (goodsValue <= 11) {
            if (value.equals("4")) {
                // 不符合要求
                return new BigDecimal("40000000");
            }
        } else if (goodsValue <= 14) {
            if (value.equals("5")) {
                // 不符合要求
                return new BigDecimal("50000000");
            }
        }
        return null;
    }

    // 判断当前品质所消耗的材料
    public static String getGoodsQuality(String quality) {
        switch (quality) {
        case "把玩":
            return "1";
        case "贴身":
            return "2";
        case "珍藏":
            return "3";
        case "无价":
            return "4";
        case "传世":
            return "5";
        case "神迹":
            return "6";
        default:
            break;
        }
        return null;
    }
}
